package com.sdwl.brcl.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * TODO(一句话描述该类的功能，以英文句号结尾)
 *
 * @version v1.0
 * @className: TimeUtil
 * @author: heyiheng
 * @date: 2020-11-28 15:57
 */
public class TimeUtil {
	/**
	 * 获取当天的日期（2020-03-04）
	 * @return
	 */
	public static String getDateNow(){
		LocalDate localDate = LocalDate.now();
		return localDate.toString();
	}

	/**
	 * 获取当天的日期（指定格式）
	 * @param formatter 如yyyy/MM/dd，yyyy年MM月dd日
	 * @return
	 */
	public static String getDateNow(String formatter){
		LocalDate localDate = LocalDate.now();
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern(formatter);
		return localDate.format(formatters);
	}

	/**
	 * 获取增加几天后的日期
	 * @param days
	 * @return
	 */
	public static String getDatePlusDay(int days){
		LocalDate localDate = LocalDate.now().plusDays(days);
		return localDate.toString();
	}

	/**
	 * 获取增加几天后的日期（指定格式）
	 * @param formatter 如yyyy/MM/dd，yyyy年MM月dd日
	 * @return
	 */
	public static String getDatePlusDay(int days,String formatter){
		LocalDate localDate = LocalDate.now().plusDays(days);
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern(formatter);
		return localDate.format(formatters);
	}

	/**
	 * 获取减几天后的日期
	 * @param days
	 * @return
	 */
	public static String getDateminusDay(int days){
		LocalDate localDate = LocalDate.now().minusDays(days);
		return localDate.toString();
	}

	/**
	 * 获取减几天后的日期（指定格式）
	 * @param formatter 如yyyy/MM/dd，yyyy年MM月dd日
	 * @return
	 */
	public static String getDateminusDay(int days,String formatter){
		LocalDate localDate = LocalDate.now().minusDays(days);
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern(formatter);
		return localDate.format(formatters);
	}

	/**
	 * 获取当前时间
	 * @return 2020-03-04 09:56:17
	 */
	public static String getDateTimeNow(){
		LocalDateTime localDateTime =LocalDateTime.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		return localDateTime.format(formatter);
	}

	/**
	 * 一个日期增加几天后的日期的零点时间戳（毫秒）
	 * @param days
	 * @param formatter
	 * @return
	 */
	public static long getTimeStampMilliPlusDay(String date,int days,String formatter){
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern(formatter);
		LocalDate localDate = LocalDate.parse(date,formatters).plusDays(days);
		LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return instant.toEpochMilli();
	}

	/**
	 * 一个日期增加几天后的日期的零点时间戳（毫秒）
	 * @param days
	 * @param formatter
	 * @return
	 */
	public static long getTimeStampSecondPlusDay(String date,int days,String formatter){
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern(formatter);
		LocalDate localDate = LocalDate.parse(date,formatters).plusDays(days);
		LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return instant.getEpochSecond();
	}

	/**
	 * 获取当前时间戳（秒）
	 * @return
	 */
	public static long getTimeStampSecond(){
		Instant instant = Instant.now();
		return instant.getEpochSecond();
	}

	/**
	 * 获取当前时间戳（毫秒）
	 * @return
	 */
	public static long getTimeStampMillisecond(){
		Instant instant = Instant.now();
		return instant.toEpochMilli();
	}

	/**
	 * 时间戳转日期时间（毫秒）
	 * @param timeStamp
	 * @param formatter
	 * @return
	 */
	public static String getDateTimeByTimeStampMilli(long timeStamp,String formatter){
		Instant instant = Instant.ofEpochMilli(timeStamp);
		ZoneId zone = ZoneId.systemDefault();
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
		DateTimeFormatter formatters;
		if (formatter!=null){
			formatters = DateTimeFormatter.ofPattern(formatter);
		}else {
			formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		}
		return localDateTime.format(formatters);
	}

	/**
	 * 时间戳转日期时间（秒）
	 * @param timeStamp
	 * @param formatter
	 * @return
	 */
	public static String getDateTimeByTimeStampSecond(long timeStamp,String formatter){
		Instant instant = Instant.ofEpochSecond(timeStamp);
		ZoneId zone = ZoneId.systemDefault();
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
		DateTimeFormatter formatters;
		if (formatter!=null){
			formatters = DateTimeFormatter.ofPattern(formatter);
		}else {
			formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		}
		return localDateTime.format(formatters);
	}

	/**
	 * 时间戳转日期(毫秒)
	 * @param timeStamp
	 * @param formatter
	 * @return
	 */
	public static String getDateByTimeStampMilli(long timeStamp,String formatter){
		ZoneId zone = ZoneId.systemDefault();
		LocalDate localDate = Instant.ofEpochMilli(timeStamp).atZone(zone).toLocalDate();
		DateTimeFormatter df;
		if (formatter!=null){
			df = DateTimeFormatter.ofPattern(formatter);
		}else {
			df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		}
		return localDate.format(df);
	}

	/**
	 * 时间戳转日期(秒)
	 * @param timeStamp
	 * @param formatter
	 * @return
	 */
	public static String getDateByTimeStampSecond(long timeStamp,String formatter){
		ZoneId zone = ZoneId.systemDefault();
		LocalDate localDate = Instant.ofEpochSecond(timeStamp).atZone(zone).toLocalDate();
		DateTimeFormatter df;
		if (formatter!=null){
			df = DateTimeFormatter.ofPattern(formatter);
		}else {
			df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		}
		return localDate.format(df);
	}

	/**
	 * 获取指定日期的时间戳（秒）
	 * @param time 2020-03-04 10:05:40
	 * @param formatter yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static long getTimeStampSecondByDataTime(String time,String formatter){
		DateTimeFormatter df;
		if (formatter!=null){
			df = DateTimeFormatter.ofPattern(formatter);
		}else {
			df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		}
		LocalDateTime localDateTime = LocalDateTime.parse(time, df);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return instant.getEpochSecond();
	}

	/**
	 * 获取指定日期的时间戳（秒）
	 * @param time 2020-03-04 10:05:40
	 * @param formatter yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static long getTimeStampMilliByDataTime(String time,String formatter){
		DateTimeFormatter df;
		if (formatter!=null){
			df = DateTimeFormatter.ofPattern(formatter);
		}else {
			df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		}
		LocalDateTime localDateTime = LocalDateTime.parse(time, df);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return instant.toEpochMilli();
	}

	/**
	 * 获取当天零点的时间戳（毫秒）
	 * @return
	 */
	public static long getTimeStampMilliSameDayBegin(){
		LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return instant.toEpochMilli();
	}

	/**
	 * 获取当天零点的时间戳（秒）
	 * @return
	 */
	public static long getTimeStampSecondSameDayBegin(){
		LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return instant.getEpochSecond();
	}

	public static Date getDateDayBegin(){
		LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
		Date date = Date.from(zdt.toInstant());
		return date;
	}

	/**
	 * 获取当天结束的时间戳（毫秒）
	 * @return
	 */
	public static long getTimeStampMilliSameDayEnd(){
		LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return instant.toEpochMilli();
	}

	public static Date getDateDayEnd(){
		LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
		Date date = Date.from(zdt.toInstant());
		return date;
	}

	/**
	 * 获取当天结束的时间戳（秒）
	 * @return
	 */
	public static long getTimeStampSecondSameDayEnd(){
		LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return instant.getEpochSecond();
	}

	/**
	 * 根据日期获取当天开始时间戳（毫秒）
	 * @param date 20200304
	 * @param formatter yyyyMMdd
	 * @return
	 */
	public static long getTimeStampMilliByDate(String date,String formatter){
		DateTimeFormatter df;
		if (formatter!=null){
			df = DateTimeFormatter.ofPattern(formatter);
		}else {
			df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		}
		LocalDate localDate = LocalDate.parse(date,df);
		LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return instant.toEpochMilli();
	}

	/**
	 * 根据日期获取当天开始时间戳（秒）
	 * @param date 20200304
	 * @param formatter yyyyMMdd
	 * @return
	 */
	public static long getTimeStampSecondByDate(String date,String formatter){
		DateTimeFormatter df;
		if (formatter!=null){
			df = DateTimeFormatter.ofPattern(formatter);
		}else {
			df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		}
		LocalDate localDate = LocalDate.parse(date,df);
		LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return instant.getEpochSecond();
	}
}
